package net.lab1024.sa.user.module.business.mall.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.common.common.domain.PageResult;
import net.lab1024.sa.common.common.domain.ResponseDTO;
import net.lab1024.sa.common.common.util.SmartPageUtil;
import net.lab1024.sa.user.module.business.mall.dao.GoodsDao;
import net.lab1024.sa.user.module.business.mall.domain.entity.GoodsEntity;
import net.lab1024.sa.user.module.business.mall.domain.form.GoodsQueryForm;
import net.lab1024.sa.user.module.business.mall.domain.vo.GoodsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品
 *
 * @Author 1024创新实验室: 胡克
 * @Date 2021-10-25 20:26:54
 * @Wechat zhuoda1024
 * @Email lab1024@163.com
 * @Copyright 1024创新实验室 （ https://1024lab.net ），2012-2022
 */
@Service
public class GoodsService {
    @Autowired
    private GoodsDao goodsDao;



    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public ResponseDTO<PageResult<GoodsVO>> query(GoodsQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<GoodsEntity> list = goodsDao.query(page, queryForm);
        List<GoodsVO> goodsVOList=list.stream().map(goodsEntity->{
            GoodsVO goodsVO=new GoodsVO();
            BeanUtils.copyProperties(goodsEntity,goodsVO);
            return goodsVO;
        }).collect(Collectors.toList());

        PageResult<GoodsVO> pageResult = SmartPageUtil.convert2PageResult(page, goodsVOList);
        if (pageResult.getEmptyFlag()) {
            return ResponseDTO.ok(pageResult);
        }

        return ResponseDTO.ok(pageResult);
    }
}
